﻿using System;
using System.Linq;
using System.Web.Mvc;
using Isaac.Joy.Identity.Module;
using Isaac.Joy.Identity.Web.DtCms.Models;
using Isaac.Joy.StringHelper;
using Newtonsoft.Json;

namespace Isaac.Joy.Identity.Web.DtCms.Controllers
{
    public class UserController : RepositoryController
    {
        /// <summary>
        /// 用户查看管理页面
        /// </summary>
        /// <param name="pi"></param>
        /// <param name="ps"></param>
        /// <param name="wd"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult Index(int? pi, int? ps, string wd, int? rid)
        {
            var pageindex = pi ?? 1;
            if (pageindex <= 0) pageindex = 1;
            var pagesize = ps ?? 10;
            if (pagesize <= 0) pagesize = 1;

            int datacount;
            var list = Repository.QueryUser(pageindex, pagesize, out datacount, wd, rid);

            return View(new UserListViewModel
            {
                PageInfor = new PageInfor(pageindex, pagesize, datacount),
                KeywordFliter = wd,
                RoleFliter = rid,
                Data = list,
                Roles = Repository.GetRoles()
            });
        }

        /// <summary>
        /// 用户添加页面
        /// </summary>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult Add()
        {
            return View(new UserViewModel
            {
                User = new User(),
                Roles = Repository.GetRoles()
            });
        }

        /// <summary>
        /// 添加用户的Ajax处理方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult AjaxAdd(UserViewModel model)
        {
            var user = Repository.AddUser(model.User);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = user != null
            }));
        }

        /// <summary>
        /// 删除用户的Ajax处理方法
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult AjaxDelete(string strId)
        {
            var ids = strId.SplitToInt(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var rowcount = Repository.DeleteUser(ids.ToArray());

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = rowcount > 0
            }));
        }

        /// <summary>
        /// 编辑修改用户页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult Edit(int id)
        {
            var user = Repository.GetUser(id);
            if (user == null) return HttpNotFound();

            return View(new UserViewModel
            {
                User = user,
                Roles = Repository.GetRoles()
            });
        }

        /// <summary>
        /// 修改用户的Ajax处理方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Permission("UserManage")]
        public ActionResult AjaxEdit(UserViewModel model)
        {
            var success = Repository.EditUser(model.User);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success
            }));
        }

        /// <summary>
        /// 修改个人密码页面
        /// </summary>
        /// <returns></returns>
        [Permission("PwdManage")]
        public ActionResult Pwd()
        {
            var user = Repository.GetUser(User.Identity.Name.ToInt32(0));

            return View(new UserPwdModel
            {
                Account = user.Account
            });
        }

        /// <summary>
        /// 修改密码的Ajax处理方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Permission("PwdManage")]
        public ActionResult AjaxPwd(UserPwdModel model)
        {
            var user = Repository.AuthorizeUser(User.Identity.Name.ToInt32(0), model.OldPassword);

            if (user == null)
            {
                return Content(JsonConvert.SerializeObject(new
                {
                    return_code = 1,
                    msg = "您输入的密码不正确！"
                }));
            }

            var success = Repository.EditPwd(user.Id, model.Password);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success
            }));
        }

        /// <summary>
        /// 修改个人资料页面
        /// </summary>
        /// <returns></returns>
        [Permission("InfoManage")]
        public ActionResult Info()
        {
            var user = Repository.GetUser(User.Identity.Name.ToInt32(0));
            if (user == null) return HttpNotFound();

            return View(new UserViewModel
            {
                User = user
            });
        }
        
        /// <summary>
        /// 修改个人资料的Ajax操作方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Permission("InfoManage")]
        public ActionResult AjaxEditInfo(UserViewModel model)
        {
            var success = Repository.EditInfo(model.User);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success
            }));
        }
    }
}
